package fr.bordeaux1.planingdesetudiants.view;

import java.sql.SQLException;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import fr.bordeaux1.planingdesetudiants.controler.CreneauFormater;
import fr.bordeaux1.planingdesetudiants.controler.db.IDBPlaningDesEtudiants;
import fr.bordeaux1.planingdesetudiants.model.Cours;

public class EdtViewer2 implements IEdtViewer {

	protected Shell shell;
	protected IFenetrePrincipale parent;
	private Composite compositeListeCours;
	private ScrolledComposite scrolledComposite;
	private Composite edt;
	private Composite[] creneaux;
	private ScrolledComposite scrollZoneEdt;

	public EdtViewer2(IFenetrePrincipale parent) {
		this.parent = parent;
		createContents();
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setMinimumSize(new Point(900, 250));
		shell.setImage(SWTResourceManager.getImage(EdtViewer2.class,
				"/icon.png"));
		shell.addDisposeListener(new DisposeListener() {
			@Override
			public void widgetDisposed(DisposeEvent evt) {
				parent.edtClosed();
			}
		});
		shell.setSize(778, 494);
		shell.setText("Visualisation de l'emplois du temps");
		shell.setLayout(new FormLayout());

		scrolledComposite = new ScrolledComposite(shell, SWT.BORDER
				| SWT.V_SCROLL);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setAlwaysShowScrollBars(true);
		scrolledComposite.setExpandVertical(true);
		FormData fd_scrolledComposite = new FormData();
		fd_scrolledComposite.right = new FormAttachment(0, 200);
		fd_scrolledComposite.bottom = new FormAttachment(100);
		fd_scrolledComposite.top = new FormAttachment(0);
		fd_scrolledComposite.left = new FormAttachment(0);
		scrolledComposite.setLayoutData(fd_scrolledComposite);

		compositeListeCours = new Composite(scrolledComposite, SWT.NONE);
		RowLayout rl_compositeListeCours = new RowLayout(SWT.VERTICAL);
		rl_compositeListeCours.marginWidth = 3;
		rl_compositeListeCours.marginBottom = 0;
		rl_compositeListeCours.fill = true;
		compositeListeCours.setLayout(rl_compositeListeCours);

		scrolledComposite.setContent(compositeListeCours);
		scrolledComposite.setMinSize(compositeListeCours.computeSize(
				SWT.DEFAULT, SWT.DEFAULT));

		DropTarget target = new DropTarget(compositeListeCours, DND.DROP_MOVE);
		Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
		target.setTransfer(types);
		target.addDropListener(new DropTargetListener() {
			Color defaultColor = null;

			@Override
			public void dropAccept(DropTargetEvent arg0) {
			}

			@Override
			public void drop(DropTargetEvent event) {
				if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
					String idCours = (String) event.data;
					System.out.println(idCours);
					try {
						parent.getDb().modifierCreneauCours(
								Integer.parseInt(idCours), 0);
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public void dragOver(DropTargetEvent arg0) {
			}

			@Override
			public void dragOperationChanged(DropTargetEvent arg0) {
			}

			@Override
			public void dragLeave(DropTargetEvent arg0) {
				compositeListeCours.setBackground(defaultColor);
			}

			@Override
			public void dragEnter(DropTargetEvent arg0) {
				if (defaultColor == null)
					defaultColor = compositeListeCours.getBackground();
				compositeListeCours.setBackground(new Color(shell.getDisplay(),
						new RGB(0, 0, 255)));
			}
		});

		// zone emplois du temps

		scrollZoneEdt = new ScrolledComposite(shell, SWT.BORDER | SWT.V_SCROLL);
		scrollZoneEdt.setExpandVertical(true);
		scrollZoneEdt.setExpandHorizontal(true);
		FormData fd_scrollZoneEdt = new FormData();
		fd_scrollZoneEdt.bottom = new FormAttachment(100, -3);
		fd_scrollZoneEdt.top = new FormAttachment(0, 5);
		fd_scrollZoneEdt.left = new FormAttachment(scrolledComposite, 5);
		fd_scrollZoneEdt.right = new FormAttachment(100, -5);

		scrollZoneEdt.setLayoutData(fd_scrollZoneEdt);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 6;

		edt = new Composite(scrollZoneEdt, SWT.NONE);
		edt.setLayout(gridLayout);

		Label horaire = new Label(edt, SWT.NONE);
		horaire.setAlignment(SWT.CENTER);
		horaire.setText("Horaire");
		GridData gd_horaire = new GridData(SWT.FILL, SWT.TOP, false, false, 1,
				1);
		gd_horaire.minimumHeight = 20;
		gd_horaire.minimumWidth = 70;
		horaire.setLayoutData(gd_horaire);
		

		for (int trancheHoraire = 0; trancheHoraire < 4; trancheHoraire++) {
			if (trancheHoraire == 2) {
				CLabel ll = new CLabel(edt, SWT.NONE);
				ll.setText("Pause");
				ll.setAlignment(SWT.CENTER);

				ll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
						1, 6));
			}
			Label heure = new Label(edt, SWT.NONE);
			heure.setAlignment(SWT.CENTER);
			heure.setText(CreneauFormater.formatHeure(trancheHoraire + 1));
			GridData gd_heure = new GridData(SWT.FILL, SWT.TOP, false, false,
					1, 1);

			gd_heure.minimumHeight = 20;
			gd_heure.minimumWidth = 70;
			heure.setLayoutData(gd_heure);
		}

		creneaux = new Composite[20];
		for (int jour = 0; jour < 5; jour++) {

			Label l = new Label(edt, 0);
			l.setText(CreneauFormater.getJour(jour));

			for (int trancheHoraire = 0; trancheHoraire < 4; trancheHoraire++) {
				GridLayout rl = new GridLayout();
				rl.numColumns = 1;
				rl.marginBottom = rl.marginLeft = rl.marginHeight = rl.marginRight = rl.marginTop = rl.marginWidth = 0;

				final Composite c = new Composite(edt, SWT.NONE | SWT.BORDER);
				// c.setBackground(new Color(shell.getDisplay(), new
				// RGB(255,255,255)));
				final int creneau = 4 * jour + trancheHoraire;
				creneaux[creneau] = c;

				GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);

				gd.minimumWidth = 120;
				gd.minimumHeight = 70;
				c.setLayoutData(gd);
				c.setLayout(rl);

				target = new DropTarget(c, DND.DROP_MOVE);
				types = new Transfer[] { TextTransfer.getInstance() };
				target.setTransfer(types);
				target.addDropListener(new DropTargetListener() {
					Color defaultColor = null;

					@Override
					public void dropAccept(DropTargetEvent arg0) {
						System.out.println("dropAccept");
					}

					@Override
					public void drop(DropTargetEvent event) {
						System.out.println("drop");
						if (TextTransfer.getInstance().isSupportedType(
								event.currentDataType)) {
							String idCours = (String) event.data;
							System.out.println(idCours);
							try {
								parent.getDb().modifierCreneauCours(
										Integer.parseInt(idCours), creneau + 1);
							} catch (SQLException e) {
								e.printStackTrace();
							}
						}
					}

					@Override
					public void dragOver(DropTargetEvent arg0) {
					}

					@Override
					public void dragOperationChanged(DropTargetEvent arg0) {
						System.out.println("dragOperationChanged");
					}

					@Override
					public void dragLeave(DropTargetEvent arg0) {
						System.out.println("dragLeave");
						c.setBackground(defaultColor);
					}

					@Override
					public void dragEnter(DropTargetEvent arg0) {
						System.out.println("dragEnter");
						if (defaultColor == null)
							defaultColor = c.getBackground();
						c.setBackground(new Color(shell.getDisplay(), new RGB(
								0, 0, 255)));
					}
				});
			}
		}

		scrollZoneEdt.setContent(edt);
		scrollZoneEdt.setMinSize(edt.computeSize(SWT.DEFAULT, SWT.DEFAULT));

	}

	@Override
	public void show() {
		shell.open();
		shell.layout();
		refreshData();
	}

	@Override
	public void close() {
		shell.close();
	}

	@Override
	public void refreshData() {
		IDBPlaningDesEtudiants db = parent.getDb();
		shell.setRedraw(false);
		for (int creneau = 0; creneau < 20; creneau++) {
			Control[] ctrls = creneaux[creneau].getChildren();
			for (Control control : ctrls) {
				control.dispose();
			}
		}
		Control[] ctrls = compositeListeCours.getChildren();
		for (Control control : ctrls) {
			control.dispose();
		}

		if (db != null) {
			try {
				List<Cours> courss = db.listerCours();
				for (Cours cours : courss) {

					if (cours.getCreneau() <= 0) {
						// dans la liste de gauche
						CoursCanvas canv = new CoursCanvas(compositeListeCours,
								cours, parent);
						canv.setLayoutData(new RowData(compositeListeCours
								.getSize().x - 10, 80));
					} else {
						// le placer dans l'emploi du temps
						Composite c = creneaux[cours.getCreneau() - 1];
						creneaux[cours.getCreneau() - 1].redraw();
						CoursCanvas canv = new CoursCanvas(c, cours, parent);
						GridData gd = new GridData(SWT.FILL, SWT.TOP, true,
								false, 1, 1);
						gd.heightHint = 80;
						gd.verticalSpan = 0;
						canv.setLayoutData(gd);
					}
				}
				
				shell.redraw();
				
				scrolledComposite.setContent(compositeListeCours);
				scrolledComposite.setMinSize(compositeListeCours.computeSize(
						SWT.DEFAULT, SWT.DEFAULT));

				scrollZoneEdt.setContent(edt);
				scrollZoneEdt.setMinSize(edt.computeSize(SWT.DEFAULT,
						SWT.DEFAULT));
				shell.redraw();

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		shell.setRedraw(true);
	}
}
